package com.rabbit.route;

import java.io.IOException;
import java.util.Random;
import java.util.UUID;

import com.rabbit.RabbitFactory;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

/**
 * @author CUIJIAJUN
 * @date 2017年2月15日
 * 
 */
// 随机发送6条随机类型（routing key）的日志给转发器~~
public class SendLogDirect {
	// 交换名称
	private static final String EXCHANGE_NAME = "ex_logs_direct";
	// 日志分类
	private static final String[] SEVERITIES = { "info", "warning", "error" };

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		Connection connection = RabbitFactory.creatConnection();
		Channel channel = connection.createChannel();
		// 声明转发器类型
		boolean durable = true;
		channel.queueDeclare(EXCHANGE_NAME, durable, false, false, null);
		channel.exchangeDeclare(EXCHANGE_NAME, "direct");
		// 发送6条消息
		for (int i = 0; i < 6; i++) {
			String severity = getSeverity();
			String message = severity + "_log" + UUID.randomUUID().toString();
			channel.basicPublish(EXCHANGE_NAME, severity, null,
					message.getBytes());
            System.out.println(" [x] Sent '" + message + "'");    

		}
		channel.close();
		connection.close();
	}

	/**
	 * 随机产生一种日志类型
	 * 
	 * @return
	 */
	private static String getSeverity() {
		Random random = new Random();
		int ranVal = random.nextInt(3);
		return SEVERITIES[ranVal];
	}
}
